package com.huawei.deveco.common.security.encrypt;

import com.huawei.deveco.common.security.exception.CryptoException;
import com.huawei.deveco.common.security.util.ByteUtil;

import java.nio.charset.StandardCharsets;

public interface CryptoKit {
    default String encrypt2Hex(byte[] key, byte[] msgBytes) throws CryptoException {
        return ByteUtil.bytes2Hex(encrypt(key, msgBytes));
    }

    default String encrypt2Hex(byte[] key, String msg) throws CryptoException {
        if (msg == null)
            throw new CryptoException("Illegal data");
        return ByteUtil.bytes2Hex(encrypt(key, msg.getBytes(StandardCharsets.UTF_8)));
    }

    byte[] encrypt(byte[] paramArrayOfbyte1, byte[] paramArrayOfbyte2) throws CryptoException;

    default String decryptHex2Str(byte[] key, String hexEncryptedMsg) throws CryptoException {
        return new String(decryptHex(key, hexEncryptedMsg), StandardCharsets.UTF_8);
    }

    default byte[] decryptHex(byte[] key, String hexEncryptedMsg) throws CryptoException {
        if (!ByteUtil.isHexBytes(hexEncryptedMsg))
            throw new CryptoException("Illegal hex data");
        return decrypt(key, ByteUtil.hex2bytes(hexEncryptedMsg));
    }

    byte[] decrypt(byte[] paramArrayOfbyte1, byte[] paramArrayOfbyte2) throws CryptoException;
}
